package com.mezz.bones.im.logic.service.impl;

import com.mezz.bones.im.common.exception.ImBaseException;
import com.mezz.bones.im.common.result.ImResultCode;
import com.mezz.bones.im.logic.domain.entity.ChatGroupInfo;
import com.mezz.bones.im.logic.domain.entity.ChatGroupMember;
import com.mezz.bones.im.logic.domain.entity.ChatGroupMemberRecord;
import com.mezz.bones.im.logic.domain.enums.GroupMemberRoleEnum;
import com.mezz.bones.im.logic.domain.request.GroupMemberJoinRequest;
import com.mezz.bones.im.logic.repository.IChatGroupInfoRepository;
import com.mezz.bones.im.logic.repository.IChatGroupMemberRecordRepository;
import com.mezz.bones.im.logic.repository.IChatGroupMemberRepository;
import com.mezz.bones.im.logic.repository.IChatUserRepository;
import com.mezz.bones.im.logic.service.IChatGroupMemberService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @Author: zhuang
 * @Date: 2025/6/11 17:57
 * @Description: TODO
 * @Version: 1.0
 **/

@Service
@Slf4j
@AllArgsConstructor
public class ChatGroupMemberServiceImpl implements IChatGroupMemberService {

    private final IChatGroupMemberRepository chatGroupMemberRepository;
    private final IChatGroupMemberRecordRepository chatGroupMemberRecordRepository;
    private final IChatUserRepository chatUserRepository;
    private final IChatGroupInfoRepository chatGroupInfoRepository;

    @Override
    public void joinGroup(GroupMemberJoinRequest request) {

        String userId = request.getUserId();
        String groupId = request.getGroupId();

        // 1. 检查用户是否存在 先查出对象再判断
        var user = chatUserRepository.getById(userId);
        if (user == null) {
            throw new ImBaseException(ImResultCode.DATA_NOT_FOUND,"用户不存在");
        }

        // 2. 检查群是否存在 先查出对象再判断
        ChatGroupInfo group = chatGroupInfoRepository.getById(groupId);
        if (group == null) {
            throw new ImBaseException(ImResultCode.DATA_NOT_FOUND,"群不存在");
        }

        // 3. 检查用户是否已经在群中
        ChatGroupMember member = chatGroupMemberRepository.getByUserIdAndGroupId(userId, groupId);
        if (member != null && member.getInGroup() == 1) {
            //记录日志 已经在群里
            log.info("用户:{}, 已经在群:{}中", userId, groupId);
            return;
        }

        // 3. 添加入群记录
        ChatGroupMemberRecord record = new ChatGroupMemberRecord();
        record.setGroupId(request.getGroupId());
        record.setUserId(request.getUserId());
        record.setOperationType(1);
        record.setOperatorId(request.getOperatorId());
        record.setOperatorName(request.getOperatorName());
        record.setOperationReason(request.getOperationReason());
        Date now = new Date();
        record.setCreateTime(now);
        record.setUpdateTime(now);
        record.setIsDel(0);
        chatGroupMemberRecordRepository.save(record);

        if(member == null) {
            //创建
            member = new ChatGroupMember();
            member.setGroupId(groupId);
            member.setUserId(userId);
            member.setInGroup(1);
            member.setMemberRole(GroupMemberRoleEnum.NORMAL.getRole());
            member.setJoinTime(now);
            member.setCreateTime(now);
            member.setUpdateTime(now);
            member.setIsDel(0);
            chatGroupMemberRepository.save(member);
        }else {
            // 更新用户在群的最新状态数据
            ChatGroupMember update = new ChatGroupMember();
            update.setId(member.getId());
            update.setInGroup(1);
            update.setJoinTime(now);
            update.setUpdateTime(now);
            update.setIsDel(0);
            chatGroupMemberRepository.updateById(update);
        }
    }
}
